package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.AppConfig;
import com.monkeygroup.mygoplus.common.DefinedView.MessageChoosePhotoDialog;
import com.monkeygroup.mygoplus.common.DefinedView.MyAlertDialog;
import com.monkeygroup.mygoplus.common.utils.FSDateTime;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.common.utils.YFileManager;
import com.monkeygroup.mygoplus.common.utils.image.Images;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.PersonHttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;
import com.monkeygroup.mygoplus.view.home.activity.ImageActivity;
import com.monkeygroup.mygoplus.view.home.activity.MainActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 个人资讯
 * Created by cumt on 2017/6/20.
 */

public class PersonInformationActivity extends BaseActivity implements View.OnClickListener  {

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonInformationActivity.class);

        activity.startActivity(intent);
    }

    private Activity activity;
    private ImageView imgBack,img_bind_type;
    private TextView txtTitle;

    private RelativeLayout rel_identify,rel_bonus,rel_login_out,rel_recommend,rel_user_name,rel_change_pwd,rel_apply_account,rel_bank,rel_person_state,rel_mobile_phone,rel_email,rel_bind_type;
    private LinearLayout ll_change_img;

    private TextView txt_user_name,txt_bind_account,txt_recommend_name,txt_person_state,txt_mobile_phone,txt_email,txt_user_id,txt_identify_state;
   private SimpleDraweeView sdv_2,sdv_backgroud;

    private PopupWindow pop;
    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;
    private PersonalNomalInfo vo;

    private PersonHttpModel httpLoginModel;
    private PersonHttpModel personHttpModel;
    private PersonalNomalInfo user;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_information);
    }

    @Override
    public void initView() {

        activity=PersonInformationActivity.this;
        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("个人資訊");

        txt_user_id= (TextView) findViewById(R.id.txt_user_id);

        rel_user_name= (RelativeLayout)findViewById(R.id.rel_user_name);
        rel_identify= (RelativeLayout)findViewById(R.id.rel_identify);
        rel_bonus= (RelativeLayout)findViewById(R.id.rel_bonus);
        rel_change_pwd = (RelativeLayout)findViewById(R.id.rel_change_pwd);
        rel_apply_account= (RelativeLayout)findViewById(R.id.rel_apply_account);
        rel_bank= (RelativeLayout)findViewById(R.id.rel_bank);
        rel_person_state= (RelativeLayout)findViewById(R.id.rel_person_state);
        rel_mobile_phone= (RelativeLayout)findViewById(R.id.rel_mobile_phone);
        rel_email= (RelativeLayout)findViewById(R.id.rel_email);
        rel_bind_type= (RelativeLayout)findViewById(R.id.rel_bind_type);
        rel_recommend= (RelativeLayout)findViewById(R.id.rel_recommend);
        rel_login_out= (RelativeLayout)findViewById(R.id.rel_login_out);

        ll_change_img= (LinearLayout)findViewById(R.id.ll_change_img);

        txt_user_name= (TextView)findViewById(R.id.txt_user_name);
        txt_bind_account= (TextView)findViewById(R.id.txt_bind_account);
        txt_recommend_name= (TextView)findViewById(R.id.txt_recommend_name);
        txt_person_state= (TextView)findViewById(R.id.txt_person_state);
        txt_mobile_phone= (TextView)findViewById(R.id.txt_mobile_phone);
        txt_email= (TextView)findViewById(R.id.txt_email);
        txt_identify_state= (TextView)findViewById(R.id.txt_identify_state);
         sdv_2= (SimpleDraweeView)findViewById(R.id.sdv_2);
        sdv_backgroud= (SimpleDraweeView)findViewById(R.id.sdv_backgroud);
        img_bind_type= (ImageView) findViewById(R.id.img_bind_type);
        //isbindThird();

        params = new HashMap<>();
        initRequest();
        initPopwindow();

        user = LocalInfo.getUserInfo(activity);
        Log.e("getMobilegetMobile===",""+user.getMobile());
        params.put("mobile",user.getMobile());
        params.put("appacc",user.getMobile());

        httpModel.startRequest();
        personHttpModel.requestUserInfo();
    }

    @Override
    public void setListener() {

    }

    private void initRequest(){


        httpModel = new HttpModel(RequestUrl.baseUrl+RequestUrl.userinfourl,params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (VilidateUtil.isSuccess(resultCode)) {
                    txt_user_id.setText(String.valueOf(((Map)data).get("userID")));
                    txt_mobile_phone.setText(String.valueOf(((Map)data).get("mobile")));
                    txt_user_name.setText(String.valueOf(((Map)data).get("name")));
                    txt_email.setText(String.valueOf(((Map)data).get("email")));
                    txt_person_state.setText(String.valueOf(((Map)data).get("status")));
                    Log.e("String.valueOf(a)))",String.valueOf(((Map)data).get("headIcon")));
                    String headPic = String.valueOf(((Map)data).get("headIcon"));
                    String background = String.valueOf(((Map)data).get("background"));
                    sdv_2.setBackgroundDrawable(new BitmapDrawable());
                    Images.with(activity).load(Uri.parse(headPic)).resize(130,90).into(sdv_2);
                    Images.with(activity).load(Uri.parse(background)).resize(130,90).into(sdv_backgroud);
                }
                else{

                }

            }
        },true);


        personHttpModel = new PersonHttpModel(RequestUrl.isappaccbind,params,new PersonHttpModel.SimpleOnHttpRequestListener(){


            @Override
            public void onRequestFinished(String resultCode, Integer timestamp) {

            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int isbind, String acc) {

                Log.e("resultCoderesultCode===",""+resultCode);
                //請求成功
                if("ERROR_OK".equals(resultCode)){
                    //已綁定
                    if(isbind==1){

                        txt_bind_account.setText("已綁定");

                    }
                    else{
                        txt_bind_account.setText("未綁定");

                    }

                }
            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int flag, String flag_name, int secflag, String secflag_name) {

            }

        },true);

        httpLoginModel = new PersonHttpModel(RequestUrl.appLoginUrl, params, new PersonHttpModel.SimpleOnHttpRequestListener() {

            @Override
            public void onRequestFinished(String resultCode, Integer timestamp) {

            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int isbind, String acc) {


            }

            @Override
            public void onRequestFinished(String resultCode, int seq, int flag, String flag_name, int secflag, String secflag_name) {
                //ERROR_OK
                if("ERROR_UNREGISTER".equals(resultCode)){
                    if(flag==2){
                        txt_identify_state.setText("正在审核");
                    }
                }

                if("ERROR_OK".equals(resultCode)){

                        txt_identify_state.setText(flag_name);

                }
            }
        }, true);


    }

    private void initPopwindow(){
        LayoutInflater myinflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView=myinflater.inflate(R.layout.popwindow_choose_image_way, null);


        pop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);

        pop.setTouchable(true);
        //  ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.backgroud_light_I));
        pop.setBackgroundDrawable(new BitmapDrawable());

        LinearLayout ll_camera = (LinearLayout)contentView.findViewById(R.id.ll_camera);
        LinearLayout ll_choose_head_pic = (LinearLayout)contentView.findViewById(R.id.ll_choose_head_pic);
        LinearLayout ll_camera_main_img = (LinearLayout)contentView.findViewById(R.id.ll_camera_main_img);
        LinearLayout ll_choose_main_img = (LinearLayout)contentView.findViewById(R.id.ll_choose_main_img);
        LinearLayout ll_choose_cancel = (LinearLayout)contentView.findViewById(R.id.ll_choose_cancel);

        ll_choose_head_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop.dismiss();
                ImageActivity.gotoActivity(activity,"2");
            }
        });
        ll_choose_main_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        pop.dismiss();
                        ImageActivity.gotoActivity(activity,"3");
                    }
                });

        ll_choose_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
    }

    @Override
    public void doEvent() {


        rel_user_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonChangeNameActivity.gotoActivity(activity,String.valueOf(txt_user_name.getText()));
            }
        });

//        rel_identify.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if("未綁定".equals(String.valueOf(txt_bind_account.getText()))){
//                    if(user.getSeq()!=0){
//                        params.put("account",user.getAccount());
//                        params.put("passwd",user.getPassword());
//                        httpLoginModel.requestUserLogin();
//
//                    }
//                    else{
//                        PersonChooseActivity.gotoActivity(activity);
//                    }
//
//                }
//                else{
//                    Toast.makeText(activity, "已綁定賬號不能修改身份",
//                            Toast.LENGTH_SHORT).show();
//
//                }
//
//            }
//        });
        rel_change_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonChangePwdActivity.gotoActivity(PersonInformationActivity.this);

            }
        });
        rel_apply_account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if("未綁定".equals(String.valueOf(txt_bind_account.getText()))){
                    PersonBindAccountActivity.gotoActivity(PersonInformationActivity.this);
                }
                else{
                    Toast.makeText(activity, "已綁定賬號",
                            Toast.LENGTH_SHORT).show();
                }



            }
        });
        rel_recommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                PersonRecommendActivity.gotoActivity(PersonInformationActivity.this,"1");
                Toast.makeText(activity, "暫未開放，敬親期待",
                        Toast.LENGTH_SHORT).show();

            }
        });
        rel_bonus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "暫未開放，敬親期待",
                        Toast.LENGTH_SHORT).show();
            }
        });
        rel_person_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonStateActivity.gotoActivity(PersonInformationActivity.this);

            }
        });
        rel_mobile_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonChangeTelphoneActivity.gotoActivity(PersonInformationActivity.this,String.valueOf(txt_mobile_phone.getText()));

            }
        });
        rel_login_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                MessageChoosePhotoDialog.show(activity, "登出", "你即將登出此賬號", "取消",
                        "確定", new MessageChoosePhotoDialog.OnButtonClickListener() {

                            @Override
                            public void onYesClick(TextView txtMessageText) {
                                // TODO Auto-generated method stub
                                //
                                MainActivity.gotoActivity(activity);
                                MyGoApplication.loginFlag=false;
                                finish();
                            }


                            @Override
                            public void onNoClick() {
                                // TODO Auto-generated method stub
                                // YTools.showPicker(activity);

                            }
                        }, true);

            }
        });
        rel_bind_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             PersonBindThirdActivity.gotoActivity(activity);


            }
        });
        rel_email.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonChangeEmailActivity.gotoActivity(activity);
            }
        });
        rel_bank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "暫未開放銀行卡綁定，敬親期待",
                        Toast.LENGTH_SHORT).show();
            }
        });

        ll_change_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (pop != null && pop.isShowing()) {
                    pop.dismiss();
                    } else {
//                        pop.showAtLocation(getCurrentFocus(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                    pop.showAsDropDown(ll_change_img);

                }
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("requestCode==",""+requestCode);
        if (requestCode == 100 && data != null) {

            String name = data.getStringExtra("name");
            txt_user_name.setText(name);

        }
        else  if(requestCode == 1 && data != null){

            String selectedImages = data.getStringExtra("selectedImages");
            String type = data.getStringExtra("type");
            if("2".equals(type)){
                sdv_2.setBackgroundDrawable(new BitmapDrawable());

                Images.with(activity).load(Uri.parse(selectedImages.replaceAll(",",""))).resize(130,90).into(sdv_2);
            }
            else if("3".equals(type)){
                sdv_backgroud.setBackgroundDrawable(new BitmapDrawable());
                Images.with(activity).load(Uri.parse(selectedImages.replaceAll(",",""))).resize(130,90).into(sdv_backgroud);
            }


        }
        else   if(requestCode == 200 && data != null){

            String status = data.getStringExtra("status");
            txt_person_state.setText(status);
        }

        else   if(requestCode == 300 && data != null){

            String phone = data.getStringExtra("phone");
            txt_mobile_phone.setText(phone);
        }
        else  if(requestCode == 101 && data != null){

            txt_bind_account.setText("已綁定");

        }

        else   if(requestCode == 400 && data != null){

            String email = data.getStringExtra("email");
            txt_email.setText(email);
        }


    }

    private void isbindThird(){
        OkHttpUtils.post().url(RequestUrl.isbind).addParams("acc",user.getAccount()).build().execute(new Callback() {
            @Override
            public Object parseNetworkResponse(Response response, int i) throws Exception {
                return null;
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                Log.e("+++","访问失败");
            }

            @Override
            public void onResponse(Object o, int i) {
                /*Gson gson = new Gson();
                AccountInfo accountInfo = gson.fromJson(gson.toJson(o), new TypeToken<AccountInfo>() {}.getType());*/
                Log.e("+++",o.toString());
            }
        });
    }

}
